namespace mynessl.APU
{
    using System;
    using System.Threading;
    using AHD.MyNes.Nes;
    using Sine;

    public class SecondaryBuffer:IDisposable
    {
        private readonly BufferDescription description;
        private readonly NesMediaStreamSource parent;
        public bool Disposed;
        public int playPosition;
        public int advance;
        private FixedMemoryStream memStream;

        private bool paused = true;

        public SecondaryBuffer(BufferDescription description, NesMediaStreamSource parent)
        {
            this.description = description;
            this.parent = parent;
            
            memStream = new FixedMemoryStream(description.BufferBytes);
            //parent.SetLoopBuffer(this);
        }

        public int WritePosition
        {
            get
            {
                return (int) memStream.Position;
            }
        }

        public FixedMemoryStream Stream
        {
            get { return memStream; }
        }

        public void Write(int pos, byte[] bytes, bool lockBuffer)
        {
            memStream.WriteFrom(pos, bytes, 0, bytes.Length, lockBuffer);
        }

        public void Play(int pos)
        {
            if (paused)
            {
                paused = false;
                //parent.InvokePlayStart();
            }
        }

        public void Stop()
        {
            if (!paused)
            {
                paused = true;
                
                //parent.InvokePlayStop();
            }
        }

        public void Dispose()
        {
            Stop();
        }
    }

    public class BufferDescription
    {
        public WaveFormat Format { get; set; }
        public bool ControlVolume;
        public int BufferBytes;

        public BufferDescription(WaveFormat format)
        {
            Format = format;
        }
    }
}